<?php

class Application_Model_UserHasEmailMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_UserHasEmails';
        $this->PropertiesMap = array
        (
            "Id"           => "user_has_email_id",
            "UserId"       => "users_user_id",
            "TypeId"       => "emails_types_emails_type_id",
            "Email"        => "user_has_email",
            "IsPrimary"    => "user_has_email_primary",
        );
    }
    
    /**
     * Get all UserEmails
     * 
     * @return Array(Application_Model_UserHasEmail)
     */
    public function fetchAll()
    {        
        $table = $this->getDbTable();
        $select = $table->select();
        $rows = $table->fetchAll($select);
        return $this->_toUserEmails($rows);
    }

    /**
     * Get a UserEmail by its ID
     * 
     * @param integer $id
     * @return Application_Model_UserHasEmail
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array(''.$this->getColumn('Id').' = ?' => $id));
        return $this->_toUserEmail($row);
    }

    /**
     * Get all UserEmails by a user ID
     * 
     * @param integer $user_id
     * @return array
     */
    public function fetchByUserId($user_id)
    {
        $rows = $this->getDbTable()->fetchAll(array(''.$this->getColumn('UserId').' = ?' => $user_id));        
        return $this->_toUserEmails($rows);
    }
    
    /**
     * Get primary UserEmail by a user ID
     * 
     * @param integer $user_id
     * @return array
     */
    public function fetchPrimaryByUserId($user_id)
    {
        $row = $this->getDbTable()->fetchRow(array(
            ''.$this->getColumn('UserId').' = ?' => $user_id,
            ''.$this->getColumn('IsPrimary').' = ?' => 1,
        ));        
        return $this->_toUserEmail($row);
    }
    
    /**
     * Set a UserEmail as primary address
     * 
     * @param integer $id
     */
    public function setAsPrimary($id)
    {
        if (null != $id)
        {
            // We need some data
            $UserEmail  = $this->fetchById($id);
            $user_id    = $UserEmail->UserId;
            
            // Remove primary from all other fields
            $data[$this->getColumn('IsPrimary')] = "0";
            $this->getDbTable()->update($data, array(''.$this->getColumn('UserId').' = ?' => $user_id));
            
            // Set primary to the requested address
            $data[$this->getColumn('IsPrimary')] = "1";
            $this->getDbTable()->update($data, array(''.$this->getColumn('Id').' = ?' => $id));
        } 
    }
    
    /**
     * Save a UserEmail
     *
     * @param Application_Model_UserHasEmail $email
     */
    public function save(Application_Model_UserHasEmail $email)
    {
        $email_id = $email->Id;
        
        $data = $this->_getTableValues($email, array("Id"));
        
        if(null == $email_id)
        {
            $email_id = $this->getDbTable()->insert($data);
        }
        else
        {
            $this->getDbTable()->update($data, array(''.$this->getColumn('Id').' = ?' => $email_id));
        }
        
        return $email_id;
    }
    
    /**
     * Save an array of UserEmail objects
     * 
     * @param Array(Application_Model_UserHasEmail)
     */
    public function saveAll($emails)
    {
        $ids    = array();
        if($emails != null && count($emails))
        {
            foreach($emails as $email)
            {
                if(($id = $this->save($email)) != null)
                    $ids[] = $id;
            }
        }
        
        return $ids;
    }
    
    /**
     * Parses UserEmail form values to an array of UserHasEmail objects
     *
     * @param array $user_emails
     * @param integer $user_id
     * @return array(Application_Model_UserHasEmail)
     */
    public function getEmailArrayFromUserEmail($user_emails, $user_id=null)
    {
        $emails = array();
        
        if(is_array($user_emails))
        {
            foreach($user_emails as $strUserEmail)
            {
                $emails[]   = $this->_getEmailFromUserEmailString($strUserEmail, $user_id);
            }
        }
        else
        {
            $emails[]   = $this->_getEmailFromUserEmailString($user_emails, $user_id);
        }
        
        return $emails;
    }
    
    /**
     * Parses UserEmail form value to an UserHasEmail object
     * 
     * @param string $strUserEmail
     * @param integer $user_id
     * @return Application_Model_UserHasEmail 
     */
    private function _getEmailFromUserEmailString($strUserEmail, $user_id=null)
    {
        $arrEmail   = explode("*", $strUserEmail);
        return new Application_Model_UserHasEmail($arrEmail[0], $user_id,  $arrEmail[1], $arrEmail[2], $arrEmail[3]);
    }
    
    /**
     * Convert a row to object
     * 
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_UserHasEmail
     */
    private function _toUserEmail(Zend_Db_Table_Row_Abstract $row = null)
    {
        // Create our object
        $object = new Application_Model_UserHasEmail();
        
        // Only go on if we actually have a row
        if($row)
        {
            // We have a propertiesmap, let's try to map it back to an object
            foreach($this->PropertiesMap as $key=>$column)
            {
                if(isset($row[$column]))
                    $object->$key      = $row[$column];
            }
        }

        return $object;
    }

    /**
     * Convert a rowset to an array of objects
     * 
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return Array(Application_Model_UserHasEmail)
     */
    private function _toUserEmails(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $results = array();
        if ($rows)
        {
            foreach ($rows as $row)
            {
                $results[] = $this->_toUserEmail($row);
            }
        }
        return $results;
    }
}